package org.ph.share.lock.test.thread2;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

/**
 * @author Hush.
 * @since 2023/5/18 12:53
 */
@Slf4j
public class Thread19 {

    static Thread thread1;
    static Thread thread2;
    static Thread thread3;

    public static void main(String[] args) throws InterruptedException {


        Park park = new Park(2);
        thread1 = new Thread(() -> {
            park.print("a", thread2);
        }, "t1");
        thread2 = new Thread(() -> {

            park.print("b", thread3);

        }, "t2");
        thread3 = new Thread(() -> {

            park.print("c", thread1);

        }, "t3");

        thread1.start();
        thread2.start();
        thread3.start();
        // 这里存在局限
        Thread.sleep(1000);
        LockSupport.unpark(thread1);

    }
}

@Slf4j
class Park {
    /**
     * 循环次数
     */
    private final int cycles;

    Park(int cycles) {
        this.cycles = cycles;
    }

    /**
     * 打印
     */
    public void print(String mess, Thread next) {
        for (int i = 0; i < cycles; i++) {
            LockSupport.park();
            log.info("mess:{}", mess);
            LockSupport.unpark(next);
        }
    }
}